#Table A8

#directory
#set working directory to the path PvP_Replication
#setwd("~/PvP_Replication")

#packages
library(tidyverse) #for data cleaning and manipulation
library(sf) # for loading shapefiles
library(conleyreg) #for estimation with conley standard errors
library(texreg) #latex tables for regression output

#load main dataset
pvp_data <- read.csv("PvP_data/PvP_data_main.csv")

#transform independent variable
#create log + 1 cultivation variable
pvp_data <- pvp_data %>% 
  mutate(logcult = log(maxcult + 1))

#subset data to municipalities with prior FARC presence
pvp_farc_subset <- pvp_data %>% filter(farc_presence == 1)

#prep weather and soil variables for iv
pvp_farc_subset <- pvp_farc_subset %>% mutate(
  draindummy = as.factor(ifelse(DRAIN == "M", 1, 0)), #moderate drainage
  humiddist = case_when(humid_mean < 85 ~ (85 - humid_mean)^2, humid_mean > 85 ~ (humid_mean - 85)^2, TRUE ~ 0 ), #distance from optimal humidity point
  temprangedist = case_when(maxtemp > 27 ~ (maxtemp - 27)^2, mintemp < 14  ~ (14 - mintemp)^2, TRUE ~ 0), #distance from optimal temp, wide band
)

#instrument for continuous independent variable
pvp_farc_subset$ols_inst <- predict(lm(logcult ~ PHAQ + temprangedist + TOTN + TOTC + sun_mean + humiddist + raintot_me + draindummy, pvp_farc_subset))


#load municipality shapefile
muni_sf <- sf::st_read("PvP_data/municipality_shapefile/municipality_shapefile.shp")


#join municipality shapefile to iv dataset
pvp_farc_subset <- left_join(muni_sf, pvp_farc_subset, by = c("mpio_ccdg2" = "municode")) %>% filter(!is.na(farc_presence))

# estimate reduced form regression with conley standard errors using varying radii 

rf_conley_1k <- conleyreg(formula = dissident_presence ~ ols_inst, data = pvp_farc_subset, dist_cutoff = 1000) #1000 km radius
rf_conley_750 <- conleyreg(formula = dissident_presence ~ ols_inst, data = pvp_farc_subset, dist_cutoff = 750) #750 km radius
rf_conley_500 <- conleyreg(formula = dissident_presence ~ ols_inst, data = pvp_farc_subset, dist_cutoff = 500) #500 km radius
rf_conley_200 <- conleyreg(formula = dissident_presence ~ ols_inst, data = pvp_farc_subset, dist_cutoff = 200) #200 km radius
rf_conley_100 <- conleyreg(formula = dissident_presence ~ ols_inst, data = pvp_farc_subset, dist_cutoff = 100) #100 km radius

#get number of observations
num_obs <- length(pvp_farc_subset$dissident_presence)

#generate latex table for results
texreg(list(rf_conley_1k, rf_conley_750, rf_conley_500, rf_conley_200, rf_conley_100), 
       digits = 2, include.ci = FALSE, single.row = FALSE, caption.above = TRUE,
       caption = "IV Reduced Form with Spatial (Conley) Standard Errors", 
       custom.gof.rows = list("Num. obs" = c(num_obs, num_obs, num_obs, num_obs, num_obs)),
       custom.header = list("1000km" = 1, "750km" = 2, "500km" = 3, "200km" = 4, "100km" = 5))

